package com.oscarmovie;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import tt.util.IOUtils;

public class MtimePerson extends Mtime {
	public static void main(String[] args) {
		//HashMap<String, String> searchSinglePerson = searchSinglePerson(1120712);System.out.println(searchSinglePerson);
		//searchAllPeople();
		updatePeopleName();
	}
	
	// From mtime ///////////////////////////////////////////
	public static void updatePeopleName() {
		List<HashMap<Object, Object>> select = Tools.retrieveObjectsWithoutName("person");
		int size = select.size();
		int i = 0;
		for (HashMap<Object, Object> hm : select) {
			int id = ((BigDecimal) hm.get("ID")).intValue();
			int mtimeId = ((BigDecimal) hm.get("MTIME_ID")).intValue();
			mtimeId = Math.abs(mtimeId);
			System.out.println("============================================================");
			System.out.println("No. " + (++i) +"/"+ size + ": " + id + ", " + mtimeId);

			HashMap<String, String> info = searchSinglePerson(mtimeId);

			Tools.updateAlias("person", id, info.get("name"));

		}
	}
	// From mtime ///////////////////////////////////////////
	public static void searchAllPeople() {
		List<HashMap<Object, Object>> select = Tools.retrieveObjectsWithMtimeIds("person");
		int size = select.size();
		int i = 0;
		for (HashMap<Object, Object> hm : select) {
			int id = ((BigDecimal) hm.get("ID")).intValue();
			int mtimeId = ((BigDecimal) hm.get("MTIME_ID")).intValue();
			System.out.println("============================================================");
			System.out.println("No. " + (++i) +"/"+ size + ": " + id + ", " + mtimeId);

			HashMap<String, String> info = searchSinglePerson(mtimeId);

			int imageId = Tools.createImage(info.get("img"),null,null,null);
			Tools.updateImageId("person", id, imageId);

		}
	}

	private static HashMap<String, String> searchSinglePerson(int mtimeId) {
		HashMap<String, String> hm = new HashMap<String, String>();
		String url = mtimePerson(mtimeId);
		System.out.println("URL: " + url);

		List<String> readFileAsList = IOUtils.readHttpUrlAsList(url, "UTF-8");
		for (String s : readFileAsList) {
			s = s.trim();
			//if(!s.equals(""))System.out.println(s);
			if (s.indexOf("movie_film_img fl") != -1) {
				
				String pattern = "<img src=";
				int p1 = s.indexOf(pattern) + pattern.length() + 1;
				int p2 = s.indexOf("\"", p1);
				String ss = s.substring(p1, p2);
				hm.put("img", "" + ss);
				
				pattern = "alt=\"";
				p1 = s.indexOf(pattern) + pattern.length();
				p2 = s.indexOf("\"", p1);
				ss = s.substring(p1, p2);
				hm.put("name", "" + ss);
			}
		}
		
		return hm;
	}

	// From baidu ///////////////////////////////////////////
	public static void searchPersonIds() {
		List<HashMap<Object, Object>> select = Tools.retrieveMtimeObjects("person");
		int size = select.size();
		int i = 0;
		for (HashMap<Object, Object> hm : select) {
			int movieId = ((BigDecimal) hm.get("ID")).intValue();
			String movieName = (String) hm.get("NAME");
			System.out.println("No. " + (++i) + "/" + size);
			int mtimeId = searchPersonMtimeId(movieName);
			Tools.updatePersonMtimeId(movieId, mtimeId);
		}
	}

	private static int searchPersonMtimeId(String key) {
		int id = 0;
		String url = baiduPersonMtime(key);
		System.out.println(url);
		List<String> readFileAsList = IOUtils.readHttpUrlAsList(url, "GBK");

		String pattern = "www.mtime.com/person/";

		for (String s : readFileAsList) {
			// System.out.println(s);
			int p1 = s.indexOf(pattern);
			if (p1 != -1) {
				p1 += pattern.length();
				int p2 = s.indexOf("\"", p1);
				String i = s.substring(p1, p2);
				try {
					id = Integer.parseInt(i);
				} catch (Exception e) {
					System.out.println("ERR: " + i + ": " + s);
				}
				break;
			}
		}
		System.out.println(id);
		return id;
	}

}
